function getQueryVariable(variable)
{
       var query = window.location.search.substring(1);
       var vars = query.split("&");
       for (var i=0;i<vars.length;i++) {
               var pair = vars[i].split("=");
               if(pair[0] == variable){return pair[1];}
       }
       return(false);
}



$(function () {
    $('#save-goods-btn').click(function (event) {
        event.preventDefault();
        var self = $(this);
        var dialog = $('#goods-dialog');
        var goods_img = $("input[name='goods_img']").val();
        var priority = $("input[name='priority']").val();

        var type_id = document.getElementsByName("type_id")[0].value;;
        var submitType = self.attr("data-type");
        var detail_id = self.attr('data-id');
        var goods_id = self.attr('data-goods');

        var args_goods_id = getQueryVariable('goods_id');
        var url = '';
        if (submitType == 'update'){
            url = '/cms/mall/update_detail/';
        }else{
            url = '/cms/mall/add_detail/?goods_id=' + args_goods_id;
        }

        console.log(type_id);
        zlajax.post({
            'url':url,
            'data':{
                'goods_id':goods_id,
                'name':name,
                'goods_img':goods_img,
                'priority':priority,
                'status':type_id,
                'detail_id':detail_id,
            },
            'success':function (data) {
                dialog.modal('hide');
                if(data['code']==200){
                    window.location.reload()
                }else{
                    const timer =setInterval(() => {
                                  zlalert.alertError(data['message']);
                                  clearInterval(timer);

                                }, 100);
                }
            },
            'fail':function () {
                zlalert.alertNetworkError();
            }
        })
    })
});


$(function () {
    $(".edit-goods-btn").click(function (event) {
        var self = $(this);
        var dialog = $('#goods-dialog');
        dialog.modal('show');

        var tr = self.parent().parent();
        var goods_img = tr.attr("data-img");
        var priority = tr.attr("data-priority");

        var imageInput = dialog.find("input[name='goods_img']");
        var priorityInput = dialog.find("input[name='priority']");

        var savebtn = dialog.find("#save-goods-btn");


        imageInput.val(goods_img);

        priorityInput.val(priority);
        // richInput.val(rich_text);
        savebtn.attr("data-type",'update');
        savebtn.attr("data-id",tr.attr('data-id'));
        savebtn.attr("data-goods",tr.attr('data-goods'));
    })
});


$(function () {
    $('.delete-goods-btn').click(function (event) {
        var self = $(this)
        var tr = self.parent().parent();
        var banner_id = tr.attr('data-id');
        zlalert.alertConfirm({
            'msg':'您确定要删除这个详情吗？',
            'confirmCallback':function () {
                zlajax.post({
                    'url':'/cms/mall/del_detail/',
                    'data':{
                        'detail_id':banner_id
                    },
                    'success':function (data) {
                        if(data['code']==200){
                            window.location.reload()
                        }else{
                           const timer =setInterval(() => {
                                  zlalert.alertError(data['message']);
                                  clearInterval(timer);

                                }, 100);
                        }
                    }
                })
            }
        })
    })
});

